package com.wxy.controller;

import com.wxy.domain.R;
import com.wxy.service.ApiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * @author 478685194
 */
@Controller
@RequestMapping("api")
public class ApiController {
    private final ApiService apiService;
    @Autowired
    public ApiController(ApiService apiService) {
        this.apiService = apiService;
    }

    /**
     * 查看图书列表
     * @param searchTerm 用户查询书籍传入的字符串
     */
    @Operation(summary = "书籍展示",description = "根据用户传入的搜索书籍")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @GetMapping("/books/{searchTerm}")
    @ResponseBody
    public R getBooks(@PathVariable("searchTerm") String searchTerm) throws Exception {
        return apiService.getBookList(searchTerm);
    }

    /**
     * 借书
     * @param bookId 书id
     * @param request web
     * @return 借书状态
     */
    @Operation(summary = "用户借书",description = "用户借书")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("/borrow/{bookId}")
    @ResponseBody
    public R borrow(@PathVariable("bookId") int bookId,HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return apiService.borrowBook(bookId,id);
    }

    /**
     * 查看借阅记录
     * @param searchTerm 用户查询借阅记录传入的书籍名称
     * @param request request
     * @return 借阅记录的集合
     */
    @Operation(summary = "借阅记录",description = "查看用户借阅记录")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @GetMapping("/borrowrecord/{searchTerm}")
    @ResponseBody
    public R getBorrowRecord(@PathVariable("searchTerm") String searchTerm,HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return apiService.getBorrowRecord(id,searchTerm);
    }

    /**
     * 还书
     */
    @Operation(summary = "用户还书",description = "用户还书")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @PostMapping("/returnbook/{borrowId}")
    @ResponseBody
    public R returnBook(@PathVariable("borrowId") String borrowId,HttpServletRequest request) {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return apiService.returnBook(borrowId,id);
    }

    /**
     * 查看用户信息
     */
    @Operation(summary = "查看用户信息",description = "用户信息查看和修改")
    @ApiResponse(responseCode = "200",description = "成功",content = @Content)
    @GetMapping("user-info")
    @ResponseBody
    public R userInfo(HttpServletRequest request) throws IOException {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return apiService.getUserInfo(id);
    }

    /**
     * 退出登录
     * @return 登录界面
     */
    @Operation(summary = "退出登录",description = "用户退出登录")
    @ApiResponse(responseCode = "200",description = "success" , content = @Content)
    @RequestMapping("outLogin")
    public String outLogin(HttpServletRequest request) throws IOException {
        Map<String,Object> usermap=(Map<String, Object>) request.getAttribute("body");
        int id=(int)usermap.get("userId");
        return apiService.outLogin(id);
    }
}
